import * as Cesium from 'cesium/Cesium'
import AxisLinePrimitive from "./AxisLinePrimitive";
import TransformAxis from "./TransformAxis";
import getWidgetOrigin from "./getWidgetOrigin";
var widgetOriginScratch$1 = new Cesium.Cartesian3
    , originScratch = new Cesium.Cartesian3
    , directionScratch$1 = new Cesium.Cartesian3
    , planeNormalScratch = new Cesium.Cartesian3
    , pickedPointScratch = new Cesium.Cartesian3
    , moveScratch$1 = new Cesium.Cartesian3
    , offsetScratch$e = new Cesium.Cartesian3
    , rayScratch$6 = new Cesium.Ray
    , noScale$3 = new Cesium.Cartesian3(1,1,1);
function getPoint(e) {
    return {
        position: TransformAxis.getValue(e),
        show: !1,
        color: TransformAxis.getColor(e),
        pixelSize: 20,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
        id: e
    }
}
function getLinePrimitive(e) {
    return new AxisLinePrimitive({
        positions: [Cesium.Cartesian3.ZERO, TransformAxis.getValue(e)],
        color: TransformAxis.getColor(e),
        id: e,
        show: !1
    })
}
function ScaleEditor(e) {
    var t = (e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT)).scene
        , i = e.transform
        , r = t.primitives.add(new Cesium.PointPrimitiveCollection);
    this.originOffset = e.originOffset,
        this._points = r,
        this._pointX = r.add(getPoint(TransformAxis.X)),
        this._pointY = r.add(getPoint(TransformAxis.Y)),
        this._pointZ = r.add(getPoint(TransformAxis.Z)),
        this._polylineX = t.primitives.add(getLinePrimitive(TransformAxis.X)),
        this._polylineY = t.primitives.add(getLinePrimitive(TransformAxis.Y)),
        this._polylineZ = t.primitives.add(getLinePrimitive(TransformAxis.Z)),
        this._scene = t,
        this._canvas = t.canvas,
        this._enableNonUniformScaling = e.enableNonUniformScaling,
        this._setPositionCallback = e.setPosition,
        this._setScaleCallback = e.setScale,
        this._modelMatrix = new Cesium.Matrix4,
        this._pickedAxis = void 0,
        this._dragAlongVector = void 0,
        this._offsetVector = new Cesium.Cartesian3,
        this._pickingPlane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X,0),
        this._dragging = !1,
        this._startPosition = new Cesium.Cartesian3,
        this._startScale = new Cesium.Cartesian3,
        this._startOffset = new Cesium.Cartesian3,
        this._startTransform = new Cesium.Matrix4,
        this._active = !1,
        this._transform = i,
        this._lineLength = 1.5 * e.radius,
        this.update()
}
Cesium.defineProperties(ScaleEditor.prototype, {
    active: {
        get: function() {
            return this._active
        },
        set: function(e) {
            (this._active = e) ? (this._pointX.show = !0,
                this._pointY.show = !0,
                this._pointZ.show = !0,
                this._polylineX.show = !0,
                this._polylineY.show = !0,
                this._polylineZ.show = !0) : (this._pointX.show = !1,
                this._pointY.show = !1,
                this._pointZ.show = !1,
                this._polylineX.show = !1,
                this._polylineY.show = !1,
                this._polylineZ.show = !1,
                this._dragging = !1)
        }
    }
}),
    ScaleEditor.prototype.handleLeftDown = function(e) {
        for (var t, i = this._scene, r = this._transform, n = i.camera, a = i.drillPick(e), 
                 o =Cesium.Matrix4.getTranslation(r, originScratch), s = 0; s < a.length; s++) {
            var l = a[s];
            if (Cesium.defined(l.id) && Cesium.defined(TransformAxis[l.id])) {
                t = l.id;
                break
            }
        }
        if (Cesium.defined(t)) {
            var c = TransformAxis.getValue(t)
                , u = Cesium.Matrix4.multiplyByPointAsVector(this._modelMatrix, c, directionScratch$1)
                , d = planeNormalScratch;
            d = .7 < Math.abs(Cesium.Cartesian3.dot(n.upWC, u)) ? Cesium.Cartesian3.cross(n.rightWC, u, d) : Cesium.Cartesian3.cross(n.upWC, u, d),
                Cesium.Cartesian3.normalize(d, d);
            var h = Cesium.Plane.fromPointNormal(o, d, this._pickingPlane)
                , p = Cesium.IntersectionTests.rayPlane(n.getPickRay(e, rayScratch$6), h, this._startPosition);
            if (Cesium.defined(p)) {
                this._offsetVector = Cesium.Cartesian3.subtract(p, o, this._offsetVector),
                    this._dragging = !0;
                var m, f = Cesium.Matrix4.getScale(r, this._startScale);
                m = t === TransformAxis.X ? f.x : t === TransformAxis.Y ? f.y : f.z,
                    this._startValue = m,
                    this._startOffset = Cesium.Cartesian3.multiplyComponents(this.originOffset, f, this._startOffset),
                    this._dragAlongVector = c,
                    this._pickedAxis = t,
                    this._startTransform = Cesium.Matrix4.setScale(r, noScale$3, this._startTransform),
                    i.screenSpaceCameraController.enableInputs = !1
            }
        }
    }
    ,
    ScaleEditor.prototype.handleMouseMove = function(e) {
        if (this._dragging) {
            var t = this._scene.camera
                , i = Cesium.IntersectionTests.rayPlane(t.getPickRay(e, rayScratch$6), this._pickingPlane, pickedPointScratch);
            if (Cesium.defined(i)) {
                var r = this._dragAlongVector
                    , n = Cesium.Matrix4.multiplyByPointAsVector(this._modelMatrix, r, directionScratch$1)
                    , a = Cesium.Cartesian3.subtract(i, this._startPosition, moveScratch$1);
                a = Cesium.Cartesian3.projectVector(a, n, a);
                var o = Cesium.Cartesian3.magnitude(a);
                if (Cesium.Cartesian3.dot(a, this._offsetVector) < 0 && (o = -o),
                    o /= this._lineLength,
                    !((o += this._startValue) <= 0)) {
                    var s = this._pickedAxis
                        , l = this._startScale;
                    this._enableNonUniformScaling() ? s === TransformAxis.X ? l.x = o : s === TransformAxis.Y ? l.y = o : l.z = o : (l.x = o,
                        l.y = o,
                        l.z = o);
                    var c = Cesium.Cartesian3.multiplyComponents(this.originOffset, l, offsetScratch$e);
                    c = Cesium.Cartesian3.subtract(this._startOffset, c, c),
                        c = Cesium.Matrix4.multiplyByPoint(this._startTransform, c, c),
                        this._setScaleCallback(l),
                        this._setPositionCallback(c)
                }
            }
        }
    }
    ,
    ScaleEditor.prototype.handleLeftUp = function() {
        this._dragging = !1,
            this._scene.screenSpaceCameraController.enableInputs = !0
    }
    ,
    ScaleEditor.prototype.update = function() {
        var e = this._transform
            , t = getWidgetOrigin(e, this.originOffset, widgetOriginScratch$1)
            , i = Cesium.Matrix4.multiplyByUniformScale(e, this._lineLength, this._modelMatrix);
        i = Cesium.Matrix4.setTranslation(i, t, i),
            this._polylineX.modelMatrix = i,
            this._polylineY.modelMatrix = i,
            this._polylineZ.modelMatrix = i,
            this._points.modelMatrix = i
    }
    ,
    ScaleEditor.prototype.isDestroyed = function() {
        return !1
    }
    ,
    ScaleEditor.prototype.destroy = function() {
        this.active = !1;
        var e = this._scene;
        this._points.removeAll(),
            e.primitives.remove(this._polylineX),
            e.primitives.remove(this._polylineY),
            e.primitives.remove(this._polylineZ),
            e.primitives.remove(this._points),
            Cesium.destroyObject(this)
    }
;
export  default  ScaleEditor;